package com.onlyforfun.exchange.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Currency;
import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.onlyforfun.exchange.R;
import com.onlyforfun.exchange.domain.TimePrice;
import com.onlyforfun.exchange.util.MoneyFormat;

public class ExchangeArrayAdapter extends ArrayAdapter<TimePrice> {
	private final ArrayList<TimePrice> list;
	private final Context context;
	int layoutResourceId;

	public ExchangeArrayAdapter(Context context, int layoutResourceId,
			ArrayList<TimePrice> data) {
		super(context, layoutResourceId, data);
		this.layoutResourceId = layoutResourceId;
		this.context = context;
		this.list = data;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View row = convertView;
		TimePriceHolder holder = null;

		if (row == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			row = inflater.inflate(layoutResourceId, parent, false);

			holder = new TimePriceHolder();
			holder.spotPurchasePrice = (TextView) row
					.findViewById(R.id.spotPurchasePrice);
			holder.name = (TextView) row.findViewById(R.id.name);
			// holder.cashPurchasePrice =
			// (TextView)row.findViewById(R.id.cashPurchasePrice);
			holder.spotOfferPrice = (TextView) row
					.findViewById(R.id.spotOfferPrice);
			// holder.cashOfferPrices =
			// (TextView)row.findViewById(R.id.cashOfferPrice);
			holder.publicTime = (TextView) row.findViewById(R.id.publicTime);
			
			holder.spotPriceLabel = (TextView)row.findViewById(R.id.spotPriceLabel);
			holder.cashPriceLabel = (TextView)row.findViewById(R.id.cashPriceLabel);
			row.setTag(holder);
		} else {
			holder = (TimePriceHolder) row.getTag();
		}
		
		TimePrice timePrice = list.get(position);
		holder.name.setText(timePrice.getName());
		holder.spotPurchasePrice.setText(MoneyFormat.valueOf(timePrice
				.getSpotPurchasePrice()));
		// holder.cashPurchasePrice.setText(String.valueOf(timePrice.getCashPurchasePrice()));
		holder.spotOfferPrice.setText(MoneyFormat.valueOf(timePrice
				.getSpotOfferPrice()));
		// holder.cashOfferPrices.setText(String.valueOf(timePrice.getCashOfferPrice()));
		SimpleDateFormat df = new SimpleDateFormat("MM.dd HH:mm:ss",
				Locale.getDefault());
		holder.publicTime.setText(df.format(timePrice.getPublicTime()));
		holder.spotPriceLabel.setText("现汇价(每100" + timePrice.getName() + ")");
		holder.cashPriceLabel.setText("现钞价(每100" + timePrice.getName() + ")");
		return row;
	}

	static class TimePriceHolder {
		public TextView cashPriceLabel;
		public TextView spotPriceLabel;
		TextView spotPurchasePrice;
		TextView name;
		// TextView cashPurchasePrice;
		TextView spotOfferPrice;
		// TextView cashOfferPrices;
		TextView publicTime;
	}
	public ArrayList<TimePrice> getListData() {
		return list;
	}
	
}
